<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/static/element/index.css}"/>
</head>
<body>
<div id="app">
    <template>
        <el-card style="background-color: #f7f7f7">
            <el-form :model="queryForm" ref="queryForm" :inline="true" label-width="100px" @submit.native.prevent>
                                                                                                                                                                            <el-form-item label="用户" prop="userId">
                                <el-input
                                        v-model="queryForm.userId"
                                        placeholder="请输入用户"
                                        clearable
                                        size="small"
                                        @keyup.enter.native="handleQuery"
                                />
                            </el-form-item>
                                                                                                                                                                                    <el-form-item label="类型" prop="type">
                                <el-select v-model="queryForm.type" placeholder="请选择类型" clearable
                                           size="small">
                                    <el-option
                                            v-for="dict in typeOptions"
                                            :key="dict.dictValue"
                                            :label="dict.dictLabel"
                                            :value="dict.dictValue"
                                    />
                                </el-select>
                            </el-form-item>
                                                                                                                                                                                    <el-form-item label="性别" prop="sex">
                                <el-select v-model="queryForm.sex" placeholder="请选择性别" clearable
                                           size="small">
                                    <el-option
                                            v-for="dict in sexOptions"
                                            :key="dict.dictValue"
                                            :label="dict.dictLabel"
                                            :value="dict.dictValue"
                                    />
                                </el-select>
                            </el-form-item>
                                                                                                                                                                                    <el-form-item label="姓名" prop="name">
                                <el-input
                                        v-model="queryForm.name"
                                        placeholder="请输入姓名"
                                        clearable
                                        size="small"
                                        @keyup.enter.native="handleQuery"
                                />
                            </el-form-item>
                                                                                                                                                                                    <el-form-item label="手机号码" prop="phone">
                                <el-input
                                        v-model="queryForm.phone"
                                        placeholder="请输入手机号码"
                                        clearable
                                        size="small"
                                        @keyup.enter.native="handleQuery"
                                />
                            </el-form-item>
                                                                                                                                                                                    <el-form-item label="地址" prop="address">
                                <el-input
                                        v-model="queryForm.address"
                                        placeholder="请输入地址"
                                        clearable
                                        size="small"
                                        @keyup.enter.native="handleQuery"
                                />
                            </el-form-item>
                                                                                                                                    
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">清空</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-row style="margin-top: 1%">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAddOrUpdate(1,null)"
                        v-if="button.add"
                >
                    新增
                </el-button>
                <el-button v-if="button.delete" size="mini" type="info" icon="el-icon-delete"
                           @click="batchHandleDelete">删除
                </el-button>
                <el-button  size="mini" type="warning" icon="el-icon-download"
                            @click="handExportData">导出
                </el-button>
            </el-col>
        </el-row>


        <el-table
                v-loading="loading"
                :data="tableData" row-key="id"
                :header-cell-style="{'text-align':'center','color':'#303133','font-weight':'600','background-color': '#F5F7FA'}"
                :cell-style="{'text-align':'center'}" border style="margin-top: 10px"
                @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" align="center"></el-table-column>
                                                                                <el-table-column label="ID" align="center" prop="id"></el-table-column>
                                                                                                <el-table-column label="用户" align="center" prop="userId"></el-table-column>
                                                                                                <el-table-column label="类型" align="center" prop="type"
                                     :formatter="typeFormat"></el-table-column>
                                                                                                <el-table-column label="性别" align="center" prop="sex"
                                     :formatter="sexFormat"></el-table-column>
                                                                                                <el-table-column label="姓名" align="center" prop="name"></el-table-column>
                                                                                                <el-table-column label="手机号码" align="center" prop="phone"></el-table-column>
                                                                                                <el-table-column label="地址" align="center" prop="address"></el-table-column>
                                                                                                <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span>{{ scope.row.createTime}}</span>
                        </template>
                    </el-table-column>
                                                                                                <el-table-column label="创建人名称" align="center" prop="createByName"></el-table-column>
                                        <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                    <el-button v-if="button.update"  size="mini" type="primary" icon="el-icon-edit"
                               @click="handleAddOrUpdate(2,scope.row)">修改
                    </el-button>

                    <el-button v-if="button.delete" size="mini" type="info" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                :page-sizes="[10, 20, 30, 40,50]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                :page-size.sync="pageSize"
                :current-page.sync="currentPage"
                @size-change="handleQuery"
                @current-change="handleQuery"
                style="float: right;margin-top: 10px"
        />
    </template>
</div>
</body>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:src="@{/static/js/request.js}"></script>
<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/api/admin/sysAddress.js}"></script>
<script th:src="@{/static/js/vue.min.js}"></script>
<script th:src="@{/static/element/index.js}"></script>
<script th:src="@{/static/js/jquery.min.js}"></script>
<script th:src="@{/static/layer/layer.js}"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                currentPage: 1,
                pageSize: 10,
                total: 0,
                tableData: [],
                multipleSelection: [],
    // 类型字典
        typeOptions: [],
    // 性别字典
        sexOptions: [],
                button: {
                    add: false,
                    update: false,
                    delete: false
                },
                loading: false,
                queryForm: {
                    roleName: ''
                }
            }
        },
        created() {
            this.handleQuery();
            let sysButtons = JSON.parse(localStorage.getItem("mars-sysButtons"));
            this.button.add = sysButtons.indexOf("admin/sysAddress/add") > -1;
            this.button.update = sysButtons.indexOf("admin/sysAddress/update") > -1;
            this.button.delete = sysButtons.indexOf("admin/sysAddress/delete") > -1;
                                                                                                        getDicts("address_type").then(response => {
                        this.typeOptions = response.data;
                    });
                                                                getDicts("sex").then(response => {
                        this.sexOptions = response.data;
                    });
                                                                                                                                                                        
        },
        methods: {
            handExportData(){
                let queryParam = this.queryForm
                queryParam.pageNo = this.currentPage
                queryParam.pageSize = -1
                exports('/admin/sysAddress/export',queryParam).then(res=>{
                    this.$notify({
                        title: '成功',
                        message: '导出成功',
                        type: 'success'
                    });
                });
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            batchHandleDelete(){
                let params = {}
                if (this.multipleSelection.length === 0) {
                    this.$notify({
                        title: '失败',
                        message: '请选择删除行',
                        duration: 2000
                    });
                    return
                }
                deleteById('/admin/sysAddress/delete/' + this.multipleSelection.map((item) => item.id), this);
            },
            switchChange(row) {
                this.formData = row
                update(this.formData).then(res => {
                    if (res.code === '200') {
                        this.$notify({
                            title: '成功',
                            message: '操作成功',
                            type: 'success'
                        });
                    }
                })
            },

            typeFormat(row, column) {
                   return selectDictLabel(this.typeOptions, row.type);
            },
            sexFormat(row, column) {
                   return selectDictLabel(this.sexOptions, row.sex);
            },
            resetQuery() {
                this.queryForm = {}
                this.handleQuery()
            },










            handleCurrentChange(val) {
                this.currentPage = val;
                this.handleQuery();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.handleQuery();
            },
            handleQuery() {
                let queryParam = this.queryForm
                queryParam.pageNo = this.currentPage
                queryParam.pageSize = this.pageSize
                this.loading = true
                pageList(queryParam).then((res) => {
                    if (res.code === '200') {
                        this.tableData = res.data.list;
                        this.total = res.data.count;
                        this.loading = false
                    } else {
                        this.loading = false;
                        this.$notify({
                            title: '失败',
                            message: res.message,
                            type: 'error'
                        });
                    }
                });
            },
            handleDelete(row) {
                deleteById('/admin/sysAddress/delete/' + row.id, this);
            },

            handleAddOrUpdate(type, row) {
                if (type === 1) {
                    openPage('新增地址设置', '/admin/sysAddress/sysAddressAddUpdate.html', this);
                } else {
                    openPage('修改地址设置', '/admin/sysAddress/sysAddressAddUpdate.html?id=' + row.id, this);
                }
            },
        }
    });
</script>
</html>
